/*
 * Copyright (C) 2011-2013 Eric Lasota
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
//#include "rdx_plugin.hpp"
//#include "rdx_programmability.hpp"
//#include "rdx_processing.hpp"
#include "rdx_constants.hpp"
#include "rdx_runtime.hpp"



namespace RDX
{
	namespace Plugins
	{
		using namespace ObjectManagement;
		using namespace Programmability;

		struct SerializationDetectorTypeSerializer : public ITypeSerializer
		{
			void DeserializeTextInstance(OperationContext *ctx, IObjectManager *objm, void *instance, IO::ITextDeserializer *td, IPackageHost *host, Package *pkg) const
			{
				*static_cast<Bool*>(instance) = TrueValue;
			}

			void DeserializeBinaryInstance(OperationContext *ctx, IObjectManager *objm, void *instance, IO::IFileStream *reader, IPackageHost *host, Package *pkg) const
			{
				*static_cast<Bool*>(instance) = TrueValue;
			}

			virtual void SerializeBinaryInstance(IObjectManager *objm, const void *obj, IO::IFileStream *fs, const ObjectManagement::SSIDTable *ssidTable) const
			{
			}

			virtual void SerializeTextInstance(IObjectManager *objm, const void *obj, IO::IFileStream *fs, const ObjectManagement::SSIDTable *ssidTable) const
			{
			}
		};

		class NumericTypesPlugin : public INativeTypeHost
		{
		private:
			Programmability::NumericTypeSerializer<Float, Float> _floatTypeSerializer;
			Programmability::NumericTypeSerializer<UShort, Float> _ushortTypeSerializer;
			Programmability::NumericTypeSerializer<Byte, Byte> _byteTypeSerializer;
			Programmability::NumericTypeSerializer<HashValue, HashValue> _hashValueSerializer;
			SerializationDetectorTypeSerializer _serializationDetectorSerializer;
		public:
			void SetTypeProperties(IObjectManager *objm, StructuredType *st) const
			{
				const String *str = GCInfo::From(st)->gstSymbol;
				if(!str)
					return;

				if(str->Equal("Core.float"))
				{
					st->_native.user.nativeAlignment = ALIGN_Float;
					st->_native.user.nativeSize = sizeof(Float);
					st->_native.user.typeSerializer = &_floatTypeSerializer;
					st->_native.user.flags |= (StructuredType::NativeProperties::UserProperties::STUF_NativeSafeToSerialize);
				}
				else if(str->Equal("Core.ushort"))
				{
					st->_native.user.nativeAlignment = ALIGN_Short;
					st->_native.user.nativeSize = sizeof(UShort);
					st->_native.user.typeSerializer = &_ushortTypeSerializer;
					st->_native.user.flags |= (StructuredType::NativeProperties::UserProperties::STUF_NativeSafeToSerialize);
				}
				else if(str->Equal("Core.byte"))
				{
					st->_native.user.nativeAlignment = ALIGN_Byte;
					st->_native.user.nativeSize = sizeof(Byte);
					st->_native.user.typeSerializer = &_byteTypeSerializer;
					st->_native.user.flags |= (StructuredType::NativeProperties::UserProperties::STUF_NativeSafeToSerialize);
				}
				else if(str->Equal("Core.serializationdetector"))
				{
					st->_native.user.nativeAlignment = ALIGN_Bool;
					st->_native.user.nativeSize = sizeof(Bool);
					st->_native.user.typeSerializer = &_serializationDetectorSerializer;
				}
				else if(str->Equal("Core.hashcode"))
				{
					st->_native.user.nativeAlignment = ALIGN_HashValue;
					st->_native.user.nativeSize = sizeof(HashValue);
					st->_native.user.typeSerializer = &_hashValueSerializer;
					st->_native.user.flags |= (StructuredType::NativeProperties::UserProperties::STUF_NativeSafeToSerialize);
				}
			}
			
			NativeCallback HookMethod(const String *str) const
			{
				return NULL;
			}
		};

		static NumericTypesPlugin i_NumericTypesPlugin;

		const INativeTypeHost *GetNumericTypesPlugin()
		{
			return &i_NumericTypesPlugin;
		}
	}
}